package com.ssm.collection.admin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ssm.collection.admin.base.BaseActionContorller;
import com.ssm.collection.admin.base.JsonResult;
import com.ssm.collection.core.SSMException;
import com.ssm.collection.core.common.dao.DynamicSqlParameter;
import com.ssm.collection.core.common.entity.user.UserEntity;
import com.ssm.collection.core.common.entity.user.UserService;
import com.ssm.collection.core.common.service.BaseService;

@Controller
@RequestMapping(value = "/user")
public class UserController extends BaseActionContorller<UserEntity> {
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	UserService userService;

	@Override
	public Logger getLog() {
		return logger;
	}

	@Override
	public BaseService<UserEntity, Long> getBaseService() {
		return userService;
	}

	@Override
	public String getTableName() {
		return "用户";
	}

	@RequestMapping(value = "get")
	@ResponseBody
	public Object get(Long id) {
		return super.get(id);
	}

	@RequestMapping(value = "list")
	@ResponseBody
	public Object getAll(UserEntity entity, Integer offset, Integer limit) {
		return super.getAll(entity, offset, limit);
	}

	@RequestMapping(value = "update")
	@ResponseBody
	public Object addOrUpdate(UserEntity entity) {
		return super.addOrUpdate(entity, entity.getId());
	}

	@ResponseBody
	@RequestMapping(value = "delte")
	public Object delete(Long id, Long operator_id, Integer is_delete) {
		return super.delete(id, operator_id, is_delete);
	}

	@RequestMapping(value = "batch")
	@ResponseBody
	public Object getUserList(HttpServletRequest request) {
		JsonResult json = new JsonResult();
		DynamicSqlParameter params = getAllParameter(request);
		List<UserEntity> users = null;
		try {
			logger.debug("查询批量用户");
			Object object = params.get("user_ids");
			if (object != null) {
				params.put("user_ids", object.toString().split(","));
			}else{
				throw new SSMException("缺少参数：user_ids");
			}

			users = userService.getBatchUser(params);
		} catch (Exception e) {
			logger.error("查询批量用户失败！");
			e.printStackTrace();
		}
		json.setData(users);
		return json;
	}
}
